package com.jz.digital.view;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import com.jz.digital.base.EditInputCallback;

/**
 * 数字、字母
 */
public class AccountInputFilter implements InputFilter {
    private EditInputCallback editInputCallback;

    public AccountInputFilter() {
    }

    public AccountInputFilter(EditInputCallback editInputCallback) {
        this.editInputCallback = editInputCallback;
    }

    public void setEditInputCallback(EditInputCallback editInputCallback) {
        this.editInputCallback = editInputCallback;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (!valid(source, start, end)) return "";
        int lineCode4 = " ".codePointAt(0);
//        int count = Math.abs(end-dend);//等于1是依次输入，大于1是复制粘贴
//        if(count>1) {
//            StringBuilder charSequence = new StringBuilder();
//            for (int i = 0; i < source.length(); i++) {
//                if(source.length()>i) {
//                    int cp = Character.codePointAt(source, i);
//                    CommonLogUtil.d("kelina====2=" + cp + " ," + source.charAt(i));
//                    if (isChinese(source.charAt(i)) || (!Character.isLetter(cp) && !Character.isDigit(cp))) {
////                        source = source.subSequence(0, i);
////                        CommonLogUtil.d("kelina====3=" + source);
//                    }else{
//                        charSequence.append(source.charAt(i));
//                    }
//                }
//            }
//            return charSequence.toString();
//
//        }else{
        int cp = Character.codePointAt(source, end - 1);
        if (isChinese(source.charAt(end - 1)) || lineCode4 == cp || (!Character.isLetter(cp) && !Character.isDigit(cp))) {
            if (editInputCallback != null) {
                editInputCallback.errorTip();
            }
            return source.subSequence(0, end - 1);
        }
//        }

        return source;
    }

    /**
     * 参数是否合法
     */
    private boolean valid(CharSequence text, int start, int end) {
        boolean success = true;
        if (TextUtils.isEmpty(text) || start < 0 || end < 0 || start >= end || start >= text.length() || end > text.length()) {
            success = false;
        }
        return success;
    }

    private boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}
